<%@ page  pageEncoding="utf-8"%>
<%@page import="microsoft.exchange.webservices.data.core.ExchangeService"%>
<%@page import="microsoft.exchange.webservices.data.credential.ExchangeCredentials"%>
<%@page import="microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion"%>
<%@page import="microsoft.exchange.webservices.data.credential.WebCredentials"%>
<%@page import="java.net.URI"%>
<%@page import="microsoft.exchange.webservices.data.core.service.item.EmailMessage"%>
<%@page import="microsoft.exchange.webservices.data.property.complex.MessageBody"%>
<%@page import="microsoft.exchange.webservices.data.core.enumeration.property.BodyType"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.File"%>
<%@page import="java.net.URLDecoder"%>
<%@page import="microsoft.exchange.webservices.data.core.service.folder.Folder"%>
<%@page import="microsoft.exchange.webservices.data.core.enumeration.property.WellKnownFolderName"%>
<%@page import="microsoft.exchange.webservices.data.property.complex.EmailAddress"%>
<%!
public static ExchangeService getExchangeService(String account, String password) throws Exception
{
    try
    {
      ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
      ExchangeCredentials credentials = new WebCredentials(account, password, "autoexpr");
      service.setCredentials(credentials);

      service.setUrl(new URI("https://mail.adient.com/EWS/Exchange.asmx"));

      return service;
    } catch (Exception e) 
    {
       e.printStackTrace(); 
       throw e;
      }
}

 public void sendMail(String account, String password,String subject, String to,  String bodyText , String from)throws Exception 
 {
 		try
 		{
 			ExchangeService service = getExchangeService(account,password);
	        EmailMessage msg = new EmailMessage(service);
	        msg.setSubject(subject);
	        MessageBody body = MessageBody.getMessageBodyFromText(bodyText);
	        body.setBodyType(BodyType.HTML);
	        msg.setBody(body);
	        
	        EmailAddress fromAddress=new EmailAddress(from);
	        msg.setFrom(fromAddress);
	        msg.getToRecipients().add(to);
	        msg.sendAndSaveCopy();
 		}catch(Exception exception)
 		{
 			exception.printStackTrace();
 			throw exception;
 		}
        
}
	public  boolean isNotBlank(String str)
	{
		if ((str==null)||("null".equalsIgnoreCase(str.trim()))||("".equals(str.trim()))) {
			return false;
		}
		return true;
	}
public  boolean isBlank(String str)
{
	if ((str==null)||("null".equalsIgnoreCase(str.trim()))||("".equals(str.trim()))) {
		return true;
	}
	return false;
}
 
%>
<%
String account=request.getParameter("account");
String password=request.getParameter("password");
//String to=request.getParameter("to");
String to="923379806@qq.com";
String from=request.getParameter("from");
String subject=request.getParameter("subject"); 
String mailBody="213123123";

try
{	
	if(isBlank(from)) {
		from="ae_china_cqyfas_oa@adient.com";
	} 
	subject=URLDecoder.decode(URLDecoder.decode(subject,"utf-8"),"utf-8");
	mailBody= "测试邮件发送";
	//sendMail(account, password,subject,null,to.split(","),to.split(","),mailBody, null,from);
	sendMail("ae_china_cqyfas_oa@adient.com","", subject, to, mailBody, from);
	out.print("{\"code\":0}");
	 
 
	
}catch(Exception exception)
{
	exception.printStackTrace();
	out.print(exception.toString());
}

%>
